#ifndef TCPSOCKET_H
#define TCPSOCKET_H

#include <memory>
#include <functional>

class TcpSocket
{
public:
    TcpSocket();
    ~TcpSocket();

    static int startup();
    static int cleanup();

    void setReadBufferSize(unsigned int size);
    unsigned int readBufferSize() const;

    bool connectToHost(std::string host, unsigned short port); // ipv4
    void abort();
    bool state() const;

    int write(const char* data);
    int write(const char* data, int len);
    int write(const std::string& data);

    unsigned int read(char *data, unsigned int maxlen);
    std::string read(unsigned int maxlen);
    std::string readAll();

    unsigned int bytesAvailable() const;

    bool isReadyRead() const;
    bool waitForReadyRead(int msecs = -1); // ms

    void registerReadyReadSig(std::function<void ()> callback); // Warning: cross-thread call

private:
    class Impl;
    std::unique_ptr<Impl> d;
};

#endif // TCPSOCKET_H
